import React from 'react'
import ReactDOM from 'react-dom'

function UserGreeting(props) {
  return <h1>Welcome back!</h1>;
};

function GuestGreeting(props) {
  return <h1>Please sogin up!</h1>;
};

function Greeting(props) {
  const isLogin = props.isLogin;
  if (isLogin) {
    return <UserGreeting />;
  }
  return <GuestGreeting />;
};

function LoginBtn(props) {
  return (
    <button onClick={props.onClick}>登陆</button>
  )
};

function LogOutBtn(props) {
  return (
    <button onClick={props.onClick}>注销</button>
  )
};

class LoginControl extends React.Component {
  constructor(props) {
    super(props);
    this.handleLoginClick = this.handleLoginClick.bind(this);
    this.handleLogoutClick = this.handleLogoutClick.bind(this);
    this.state = { isLogin: false };
  };
  handleLoginClick() {
    this.setState({
      isLogin: true
    })
  };
  handleLogoutClick() {
    this.setState({
      isLogin: false
    })
  };
  render() {
    const isLogin = this.state.isLogin;
    let button;
    if (isLogin) {
      button = <LogOutBtn onClick={ this.handleLogoutClick} />
    } else {
      button = <LoginBtn onClick={ this.handleLoginClick } />
    }
    return (
      <div>
        <Greeting isLogin={true} />
        { button }
      </div>
    )
  }
};

ReactDOM.render (
  <LoginControl />,
  document.getElementById('root')
)